//
//  DDCompleteViewController.m
//  LYJR_XDD
//
//  Created by 肖棟棟 on 16/8/6.
//  Copyright © 2016年 肖棟棟. All rights reserved.
//

#import "DDCompleteViewController.h"
#import "ZYQAssetPickerController.h"
#import "DDCompleteAuthController.h"




@interface DDCompleteViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate, ZYQAssetPickerControllerDelegate>

//头像按钮
@property (nonatomic, strong) UIButton *headImageBtn;
//点击上传头像Label
@property (nonatomic, strong) UILabel *uploadImgLabel;
//昵称view
@property (nonatomic, strong) UIView *nickView;
//昵称textField
@property (nonatomic, strong) UITextField *nickNameField;
//昵称提示的Label
@property (nonatomic, strong) UILabel *tipLabel;
//邮箱view
@property (nonatomic, strong) UIView *mailView;
//邮箱TextField
@property (nonatomic, strong) UITextField *mailField;
//开户的按钮
@property (nonatomic, strong) UIButton *openAccountBtn;


@end

@implementation DDCompleteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = DDBgColor;
    self.title = @"完善信息";
    [self createUI];
    [self setNavButton];
}



#pragma mark - 私有方法

-(void)setNavButton{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Group 51"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnDidClick)];
}

-(void)backBtnDidClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI{
    
    UIImageView *nickImageView = [[UIImageView alloc]init];
    nickImageView.image = [UIImage imageNamed:@"Group 12"];
    [self.view addSubview:nickImageView];
    
    UIImageView *mailImageView = [[UIImageView alloc]init];
    mailImageView.image = [UIImage imageNamed:@"Group 8"];
    [self.view addSubview:mailImageView];
    

    [self.view addSubview:self.headImageBtn];
    [self.view addSubview:self.uploadImgLabel];
    [self.view addSubview:self.nickView];
    [self.view addSubview:self.nickNameField];
    [self.view addSubview:self.tipLabel];
    [self.view addSubview:self.mailView];
    [self.view addSubview:self.mailField];
    [self.view addSubview:self.openAccountBtn];
    
    [_headImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //self.navigationController.navigationBar.bottom
        make.top.equalTo(self.view).offset(64+15);
        make.left.equalTo(self.view).offset(265/2.0);
        make.right.equalTo(self.view).offset(-265/2.0);
        //设置等宽等高
        make.height.equalTo(_headImageBtn.mas_width);
    }];
    
    [_uploadImgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_headImageBtn);
        make.top.equalTo(_headImageBtn.mas_bottom).offset(10.0);
    }];
    
    [_nickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_uploadImgLabel.mas_bottom).offset(25);
        make.left.mas_equalTo(self.view).offset(15);
        make.right.mas_equalTo(self.view).offset(-15);
        make.height.mas_equalTo(50);
    }];
    [nickImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nickView).offset(12);
        make.bottom.mas_equalTo(_nickView).offset(-12);
        make.left.mas_equalTo(_nickView).offset(10);
        make.width.mas_equalTo(nickImageView.mas_height);
    }];
    
    [_nickNameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(nickImageView);
        make.left.mas_equalTo(nickImageView.mas_right).offset(10);
        make.right.mas_equalTo(_nickView);
    }];

    
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(_nickView.mas_bottom).offset(2.5);
    }];
  
    [_mailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_tipLabel.mas_bottom).offset(15);
        make.left.right.height.mas_equalTo(_nickView);
    }];
    
    [mailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mailView).offset(12);
        make.bottom.mas_equalTo(_mailView).offset(-12);
        make.left.mas_equalTo(_mailView).offset(10);
        make.width.mas_equalTo(mailImageView.mas_height);
    }];
    [_mailField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(mailImageView);
        make.left.mas_equalTo(mailImageView.mas_right).offset(10);
        make.right.mas_equalTo(_mailView);
    }];
    
    
    [_openAccountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_mailView);
        make.top.equalTo(_mailView.mas_bottom).offset(25.0);
        make.height.equalTo(_mailView);
    }];
    
}


-(void)openAccountBtnDidClick{
    NSLog(@"开户");
    
    DDCompleteAuthController * caVc = [[DDCompleteAuthController alloc] init];
    
    [self.navigationController pushViewController:caVc animated:YES];
    
}



-(void)headImageBtnDicClick{
    NSLog(@"上传头像");
    
    //弹窗
    UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"nil" otherButtonTitles:@"拍照",@"从手机相册选择", nil ];
    [sheet showInView:self.view];
}


//使用手机摄像头
-(void)usePhotoCamera{
    UIImagePickerController *imagePicker =[[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    [self presentViewController:imagePicker animated:YES completion:nil];
    
}
//使用系统相册
-(void)usePgotAlbum{
    ZYQAssetPickerController *picker =[[ZYQAssetPickerController alloc]init];
    picker.navigationBar.barTintColor =[UIColor whiteColor];
    [picker.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    picker.navigationBar.translucent = NO;
    
    picker.maximumNumberOfSelection =1;
    picker.assetsFilter =[ALAssetsFilter allPhotos];
    picker.showEmptyGroups = NO;
    picker.delegate = self;
    picker.selectionFilter =[NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType]isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration =[[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration>= 5;
        }else{
            return YES;
        }
    }];
    [self presentViewController:picker animated:YES completion:nil];
    
}

#pragma mark - event action
//头像按钮被点击
-(void)headImageBtnDidClick{
    //弹窗
    UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"nil" otherButtonTitles:@"拍照",@"从手机相册选择", nil ];
    [sheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        [self usePhotoCamera];
    }else if (buttonIndex ==1){
        [self usePgotAlbum];
    }
}


#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image =[info objectForKey:UIImagePickerControllerOriginalImage];
    [_headImageBtn setImage:image forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - ZYQAssetPickerController Delegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
    for (int i = 0; i < assets.count; ++i) {
        ALAsset *set = assets[i];
        UIImage *image =[UIImage imageWithCGImage:set.defaultRepresentation.fullResolutionImage];
        [_headImageBtn setImage:image forState:UIControlStateNormal];
        // 375 *2 -265*2 = 220
        image =[CommonFunctions squareImageFromImage:image scaledToSize:110.0];
    }
    
}

#pragma mark - setter 和 getter 方法

-(UIButton *)headImageBtn{
    if (_headImageBtn ==nil) {
        _headImageBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        [_headImageBtn setImage:[UIImage imageNamed:@"Group"] forState:UIControlStateNormal];
        [_headImageBtn addTarget:self action:@selector(headImageBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headImageBtn;
}

-(UILabel *)uploadImgLabel{
    if (_uploadImgLabel == nil) {
        _uploadImgLabel =[[UILabel alloc]init];
        _uploadImgLabel.text = @"点击上传头像";
        _uploadImgLabel.font =[UIFont systemFontOfSize:24.0/2];
        _uploadImgLabel.textColor =[UIColor colorWithHexColor:@"#A7ABB4"];
        [_uploadImgLabel sizeToFit];
    }
    return _uploadImgLabel;
}

-(UIView *)nickView{
    if (_nickView == nil) {
        _nickView = [[UIView alloc] init];
        _nickView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"690 100 131 Copy"]];
        
    }
    return _nickView;
}

-(UITextField *)nickNameField{
    if (_nickNameField == nil) {
        _nickNameField = [[UITextField alloc]init];
        _nickNameField.placeholder = @"请输入昵称";
        _nickNameField.font = [UIFont systemFontOfSize:15];
         [_nickNameField setValue:[UIColor colorWithHexColor:@"#66666B"] forKeyPath:@"_placeholderLabel.textColor"];
        
    }return _nickNameField;
}

-(UILabel *)tipLabel{
    if (_tipLabel == nil) {
        _tipLabel =[[UILabel alloc]init];
        _tipLabel.text = @"昵称请控制在2-16个字符,支持中英文、数字横线和减号";
        _tipLabel.textColor =[UIColor colorWithHexColor:@"#66666B"];
        _tipLabel.font =[UIFont systemFontOfSize:12.0];
        [_tipLabel sizeToFit];
    }
    return _tipLabel;
}

-(UIView *)mailView{
    if (_mailView == nil) {
        _mailView = [[UIView alloc] init];
        _mailView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"690 100 131 Copy"]];
        
    }
    return _mailView;
}

-(UITextField *)mailField{
    if (_mailField == nil) {
        _mailField = [[UITextField alloc]init];
        _mailField.placeholder = @"请输入昵称";
        _mailField.font = [UIFont systemFontOfSize:15];
        [_mailField setValue:[UIColor colorWithHexColor:@"#66666B"] forKeyPath:@"_placeholderLabel.textColor"];
        
    }return _mailField;
}


-(UIButton *)openAccountBtn{
    if (_openAccountBtn == nil) {
        _openAccountBtn = [[UIButton alloc] init];
        [_openAccountBtn setTitle:@"开户" forState:UIControlStateNormal];
        _openAccountBtn.titleLabel.font=[UIFont systemFontOfSize:18];
        [_openAccountBtn setTitleColor:[UIColor colorWithHexColor:@"#FFCB6F"] forState:UIControlStateNormal];
        [_openAccountBtn setBackgroundImage:[UIImage imageNamed:@"Group 6"] forState:UIControlStateNormal];
        [_openAccountBtn addTarget:self action:@selector(openAccountBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _openAccountBtn;
}


@end
